const { ccclass, property } = cc._decorator;

@ccclass
export default class elevatorstep extends cc.Component {
    isCollider: boolean = false;
    ImpedeList: Map<string, boolean> = new Map<string, boolean>();
    onLoad() {
        this.node.getComponent(cc.PhysicsBoxCollider).tag = TagStep;
    }
    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact, selfCollider, otherCollider) {
        if (otherCollider.tag == TagHeroBody) {
            let normal = contact.getWorldManifold().normal;
            if (normal.y > 0.9) {
                this.onHeroContact(true);
            }
        }
        else if (otherCollider.tag == TagWayBody1 || otherCollider.tag == TagWayBody2 ) {
            var worldManifold = contact.getWorldManifold();
            var normal = worldManifold.normal;
            let event = new cc.Event.EventCustom('ImpedeInContact', true);
            if (normal.y > 0.8) {
                event.setUserData(true);
                this.ImpedeList.set(otherCollider.node.uuid, true);
                this.node.dispatchEvent(event);
                this.isCollider = true;
            }
            else if (normal.y < -0.8) {
                event.setUserData(false);
                this.ImpedeList.set(otherCollider.node.uuid, false);
                this.node.dispatchEvent(event);
                this.isCollider = true;
            }
        }
    }
    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, selfCollider, otherCollider) {
        if (this.isCollider && otherCollider.tag == TagHeroBody) {
            this.onHeroContact(false);
        }
        else if (otherCollider.tag == TagWayBody1 || otherCollider.tag == TagWayBody2 ) {
            if (this.ImpedeList.has(otherCollider.node.uuid)) {
                let bl = "";
                this.ImpedeList.delete(otherCollider.node.uuid);
                this.ImpedeList.forEach(element => {
                    if (element == true) {
                        bl += "u";
                    }
                });
                this.ImpedeList.forEach(element => {
                    if (element == false) {
                        bl += "d";
                    }
                });
                let event = new cc.Event.EventCustom('ImpedeOutContact', true);
                event.setUserData(bl);
                this.node.dispatchEvent(event);
            }
        }
    }
    onHeroContact(isUp: boolean) {
        let event = new cc.Event.EventCustom('HeroContact', true);
        event.setUserData(isUp);
        this.node.dispatchEvent(event);
        this.isCollider = isUp;
    }
    onStopStep(isStop: boolean) {
        let event = new cc.Event.EventCustom('StopStep', true);
        event.setUserData(isStop);
        this.node.dispatchEvent(event);
    }
}
